package cn.itcast.LoginInterceptor;

import cn.itcast.domain.User;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

public class LoginInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        /*获取请求的连接*/
        StringBuffer requestURL = request.getRequestURL();
        if (requestURL.indexOf("/login.do") >= 0) {
            return true;
        }
        /*获取session*/
        HttpSession session = request.getSession();
        User login_user = (User) session.getAttribute("loginUser");
        if (login_user != null) {
            return true;
        }
        request.setAttribute("msg", "你还没有登录请先登录");
        request.getRequestDispatcher("/WEB-INF/login.jsp").forward(request, response);
        return false;
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {

    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {

    }
}
